package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.vo.SkuInfoSaveVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: SkuController
 * Package: com.atguigu.gmall.product.controller
 * Description:
 *
 * @Author ltystart
 * @Create 2025/8/21 21:28
 * @Version 1.0
 */
@Slf4j
@Api(tags = "SKU管理")
@RestController
@RequestMapping("/admin/product")
public class SkuController {
    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * 获取SKU列表
     *
     * @param pn
     * @param ps
     * @return
     */
    @ApiOperation("获取SKU列表")
    @GetMapping("/list/{pn}/{ps}")
    public Result skuList(@PathVariable("pn") Long pn,
                          @PathVariable("ps") Long ps) {
        Page<SkuInfo> page = new Page<>(pn, ps);
        Page<SkuInfo> infoPage = skuInfoService.page(page);
        return Result.ok(infoPage);
    }

    /**
     * 保存SKU信息
     *
     * @param skuInfoSaveVo
     * @return
     */
    @ApiOperation("保存SKU信息")
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfoSaveVo skuInfoSaveVo) {
        log.info("保存SKU信息：{}", skuInfoSaveVo);
        skuInfoService.saveSkuInfo(skuInfoSaveVo);
        return Result.ok();
    }

    /**
     * 商品上架
     *
     * @param skuId
     * @return
     */
    @ApiOperation("商品上架")
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId) {
        skuInfoService.changeOnSale(skuId, 1);
        return Result.ok();
    }

    /**
     * 商品下架
     *
     * @param skuId
     * @return
     */
    @ApiOperation("商品下架")
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId) {
        skuInfoService.changeOnSale(skuId, 0);
        return Result.ok();
    }
}
